﻿using S2_1.Models;
using S2_1.Services;
using S2_1.Services.Models;
using System.IO;
using System.Web;
using System;
using System.Web.Http;

namespace S2_1.Controllers
{

    [Authorize]
    public class PersonalController : ApiController
    {

        /// <summary>
        /// 获取用户个人资料
        /// </summary>
        [HttpGet]
        [Route("api/profile")]
        public AppResult<ProfileOutput> Profile(string email)
        {
            using (var service = new UserService())
            {
                var profile = service.GetProfile(email);
                return profile;
            }
        }

        [HttpPost]
        [Route("api/uploadPicture")]
        public AppResult<string> UploadPicture(HttpPostedFileBase file)
        {
            if (file == null || file.ContentLength == 0)
            {
                return new AppResult<string>(false, "文件不能为空");
            }

            try
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(HttpContext.Current.Server.MapPath("~/Uploads"), fileName);
                file.SaveAs(path);
                return new AppResult<string>(true, "文件上传成功");
            }
            catch (Exception ex)
            {
                return new AppResult<string>(false, $"文件上传失败: {ex.Message}");
            }
        }
    }
}
